package com.ewanghuiju.app.presenter.pintuan;

import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.pintuan.PintuanHotGroupContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.PintuanShopListResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;

/**
 * @author sj  qq:751943509
 * @class 团
 * @time 2020/2/16
 */
public class PintuanHotGroupPresenter extends RxPresenter<PintuanHotGroupContract.View> implements PintuanHotGroupContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public PintuanHotGroupPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void attachView(PintuanHotGroupContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    /**
     * 刷新列表
     */
    private void registerEvent() {
        addSubscribe(RxBus.getDefault().toFlowable(SendEvent.class)
                .compose(RxUtil.<SendEvent>rxSchedulerHelper())
                .filter(new Predicate<SendEvent>() {
                    @Override
                    public boolean test(@NonNull SendEvent sendEvent) throws Exception {
                        return sendEvent.getType() == Constants.TYPE_GET_GROUP_INFO;
                    }
                })
                .subscribeWith(new CommonSubscriber<SendEvent>(mView, "") {
                    @Override
                    public void onNext(SendEvent s) {

                        if (mView != null)
                            mView.refreshData();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        registerEvent();
                    }
                })
        );
    }

    @Override
    public void getData(int type, int classifyId, int page, int spe_price, int sales, int people_number) {
        addSubscribe(mDataManager.getPtihotGroupList(page, spe_price == 0 ? "" : spe_price + "", sales == 0 ? "" : sales + "", people_number == 0 ? "" : people_number + "")
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<List<PintuanShopListResponBean>>>(mView) {
                    @Override
                    public void onNext(Optional<List<PintuanShopListResponBean>> optional) {
                        if (mView != null)
                            mView.showContent(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mView != null)
                            mView.showContentError();
                    }
                })
        );
    }
}
